package xworker.gluon.glisten.control;

import com.gluonhq.charm.glisten.control.BottomNavigation;
import com.gluonhq.charm.glisten.control.BottomNavigationButton;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.control.ButtonActions;
import xworker.javafx.control.ControlActions;

public class BottomNavigationActions {
    static{
        PropertyFactory.regist(BottomNavigation.class, "typeProperty", o -> {
            BottomNavigation obj = (BottomNavigation) o;
            return obj.typeProperty();
        });
        PropertyFactory.regist(BottomNavigationButton.class, "selectedProperty", o -> {
            BottomNavigationButton obj = (BottomNavigationButton) o;
            return obj.selectedProperty();
        });
    }

    public static void init(BottomNavigation node, Thing thing, ActionContext actionContext){
        ControlActions.init(node, thing, actionContext);

        if(thing.valueExists("type")){
            if("FIXED".equals(thing.getStringBlankAsNull("type"))){
                node.setType(BottomNavigation.Type.FIXED);
            }else{
                node.setType(BottomNavigation.Type.SHIFTING);
            }
        }
    }

    public static BottomNavigation create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        BottomNavigation node = new BottomNavigation();
        init(node, self, actionContext);

        actionContext.peek().put("parent", node);
        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof BottomNavigationButton){
                node.getActionItems().add((BottomNavigationButton) obj);
            }
        }

        actionContext.g().put(self.getMetadata().getName(), node);
        return node;
    }

    public static BottomNavigationButton createButton(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        BottomNavigationButton node = new BottomNavigationButton();
        ButtonActions.init(node, self, actionContext);

        actionContext.peek().put("parent", node);
        for(Thing child : self.getChilds()){
            child.doAction("create", actionContext);
        }

        actionContext.g().put(self.getMetadata().getName(), node);
        return node;
    }
}
